package org.spring.springboot.app.sys.dict.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.sys.dict.dao.SysDictDao;
import org.spring.springboot.app.sys.dict.domain.po.SysDictPO;
import org.spring.springboot.app.sys.dict.domain.vo.SysDictTypeVO;
import org.spring.springboot.app.sys.dict.domain.vo.SysTypeVO;
import org.spring.springboot.app.sys.dict.service.ISysDictService;
import org.spring.springboot.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class SysDictServiceImpl implements ISysDictService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysDictServiceImpl.class);

    @Autowired
    private SysDictDao sysDictDao;

    // key生成器,redis生成key必须用
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<SysDictPO> findAll() {
        return sysDictDao.findPage(1, Integer.MAX_VALUE, "");
    }

    public List<SysDictPO> findSysDictCache() {
        List<SysDictPO> list = redisUtils.get(Constants.DICT_REDIS_KEY);
        if (list != null) {
            return list;
        }
        list = this.findAll();
        redisUtils.set(Constants.DICT_REDIS_KEY, list, 60 * 60 * 24 * 365, TimeUnit.SECONDS);
        return list;
    }

    public void removeSysDictCache() {
        redisUtils.delete(Constants.DICT_REDIS_KEY);
    }

    @Override
    public SysDictPO find(String id) {
        return sysDictDao.selectByPrimaryKey(id);
    }

    @Override
    public List<SysDictPO> findPage(int pageNum, int pageSize, String type) {
        return sysDictDao.findPage(pageNum, pageSize, type);
    }

    @Override
    public int save(SysDictPO po) {
        po.preInsert();
        int i = sysDictDao.insertSelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int update(SysDictPO po) {
        po.preUpdate();
        int i = sysDictDao.updateByPrimaryKeySelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int delete(String id) {
        SysDictPO sysDictPO = new SysDictPO();
        sysDictPO.setId(id);
        sysDictPO.preDelete();
        int i = sysDictDao.updateByPrimaryKeySelective(sysDictPO);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public SysDictPO findByTypeValue(String type, String value) {
        SysDictPO ret = null;
        List<SysDictPO> list = this.findSysDictCache();
        if (list != null) {
            for (SysDictPO sysDictPO : list) {
                if (type.equals(sysDictPO.getType()) && value.equals(sysDictPO.getValue())) {
                    ret = sysDictPO;
                    break;
                }
            }
        }
        return ret;
    }

    @Override
    public List<SysDictPO> findByType(String type) {
        List<SysDictPO> ret = new ArrayList<SysDictPO>();
        List<SysDictPO> list = this.findSysDictCache();
        if (list != null) {
            for (SysDictPO sysDictPO : list) {
                if (type.equals(sysDictPO.getType())) {
                    ret.add(sysDictPO);
                }
            }
        }
        return ret;
    }

    @Override
    public List<SysDictTypeVO> findAllType() {
        return sysDictDao.findAllType();
    }

    @Override
    public List<SysTypeVO> findType(String type) {
        return sysDictDao.findType(type);
    }

}
